package cn.school.demo.ui.forget_password

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.fragment.findNavController
import cn.school.demo.MyApplication
import cn.school.demo.R
import cn.school.demo.databinding.FragmentForgetPasswordSecondBinding
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.google.gson.reflect.TypeToken

class ForgetPasswordSecondFragment : Fragment() {
    private lateinit var viewModel: ForgetPasswordViewModel
    private var _binding: FragmentForgetPasswordSecondBinding? = null
    private val binding get() = _binding!!

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        val map = GsonUtils.fromJson<Map<String, String>>(
            arguments?.getString("map"),
            object : TypeToken<Map<String, String>>() {}.type
        )
        LogUtils.d(map)
        viewModel = ViewModelProvider(this).get(ForgetPasswordViewModel::class.java)
        viewModel.setId(map["id"]!!)
        viewModel.setPhone(map["phone"]!!)
        _binding = DataBindingUtil.inflate(
            inflater,
            R.layout.fragment_forget_password_second,
            container,
            false
        )
        binding.view = this
        binding.vm = viewModel
        binding.lifecycleOwner = viewLifecycleOwner
        return binding.root
    }

    fun setPwd() = viewModel.setPwd(binding.forgetPasswordPasswordEditView.text.toString())
    fun setRePwd() = viewModel.setRePwd(binding.forgetPasswordRepasswordEditView.text.toString())
    fun setCode() = viewModel.setCode(binding.forgetPasswordCodeEditView.text.toString())
    fun forgetPasswordCodeButtonOnClick() {
        MyApplication.animationCountdown(binding.forgetPasswordCodeButton, null, "ForgetPassword")
        viewModel.getCodeText(viewModel.phone.value.toString())
    }

    fun forgetPasswordRepasswordButtonOnClick() {
        val toastText = viewModel.dataDetectionBySecond()
        toastText?.let { ToastUtils.showShort(toastText) } ?: let {
            viewModel.resetPwd()
            viewModel.result.observe(viewLifecycleOwner) {
                LogUtils.d("我监听到变化了！")
                when (viewModel.result.value!!.success) {
                    true -> {
                        LogUtils.d("重置密码成功！")
                        ToastUtils.showLong("重置密码成功！")
                        findNavController().navigate(R.id.action_forget_pwd_second_to_login)
                    }
                    else -> {
                        LogUtils.d(viewModel.result.value!!.message)
                        ToastUtils.showLong(viewModel.result.value!!.message)
                    }
                }
            }
        }
    }
}